package model

import (
	"errors"
	"fmt"
	"github.com/jmoiron/sqlx"
	"strconv"
	"time"
	"zimuzu_cvn_web_api/config"
	. "zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
)

//首页轮播图模块ID
const IndexSlideshowModule = 50
const AppBannerSlideshowModule = 51

type Slideshow struct {
	ID     int `db:"id" json:"id,omitempty"`
	Module int `db:"module" json:"module,omitempty"`
	//Channel    int    `db:"channel" json:"channel"`
	Title string `db:"title" json:"title,omitempty"`
	Pic   string `db:"pic" json:"pic,omitempty"`
	//Explain    int    `db:"explain" json:"explain"`
	//Resourceid int    `db:"resourceid" json:"resourceid"`
	Url    string `db:"url" json:"url,omitempty"`
	Status int    `db:"status" json:"status,omitempty"`
	//Sequence   int    `db:"sequence" json:"sequence"`
	//Operator   int    `db:"operator" json:"operator"`
	//Dateline   int    `db:"dateline" json:"dateline"`
	//TopTime    int    `db:"top_time" json:"top_time,omitempty"`
	//Down_time  int    `db:"down_time" json:"down_time"`
	Click int64 `db:"click" json:"click,omitempty"`
}

type SlideshowDB struct {
	ID       int    `db:"id" json:"id" form:"id"`
	Module   int    `db:"module" json:"module" form:"module" binding:"required"`
	Title    string `db:"title" json:"title" form:"title" binding:"required"`
	Pic      string `db:"pic" json:"pic" form:"pic"`
	Explain  string `db:"explain" json:"explain" form:"explain" binding:"required"`
	Url      string `db:"url" json:"url" form:"url" binding:"required"`
	Status   int    `db:"status" json:"status" form:"status"`
	Sequence int    `db:"sequence" json:"sequence" form:"sequence"`
	Dateline string `db:"dateline" json:"dateline"`
}

//查询web轮播图列表
//Auth: Xu
func GetSlideshow(db sqlx.Queryer, module int, page int, per int) ([]Slideshow, error) {
	res := make([]Slideshow, 0)
	start := (page - 1) * per
	err := sqlx.Select(db, &res, "select id,title, CONCAT('"+config.C.Domains.ResourcePic+"', pic) pic, url from home_admin_module where module = ? and status = 1 order by sequence desc limit ?,? ", module, start, per)
	if err != nil {
		Log("info", "轮播图查询失败！", err.Error())
		return res, err
	}

	return res, nil
}

//更新数据库轮播图点击数
//Auth: Xu
func IncrSlideShowClicks(db sqlx.Execer, id int, incrNum int) error {
	_, err := db.Exec("update home_admin_module set click = click + ? where id = ?", incrNum, id)
	if err != nil {
		Log("info", "更新数据库轮播图点击数失败", err)
		return err
	}
	return nil
}

func GetAllSlideshowList(module, page, per int) (res []SlideshowDB, err error) {
	start := (page - 1) * per
	moduleStr := "50, 51, 52, 53"
	if module > 0 {
		moduleStr = strconv.Itoa(module)
	}
	sqlStr := fmt.Sprintf("select id, module, title, CONCAT('"+config.C.Domains.ResourcePic+"', pic) pic, `explain`, url, status, sequence, "+
		"FROM_UNIXTIME(dateline, '%%Y-%%m-%%d %%H:%%i:%%s') AS dateline from home_admin_module where module IN(%s) AND `status` != 0 order by id desc limit %d,%d", moduleStr, start, per)
	err = sqlx.Select(config.C.DB.DB, &res, sqlStr)
	if err != nil {
		Log("info", "GetAllSlideshowList查询失败！", err.Error())
		return
	}

	return
}

func AddNewSlideshow(uid int64, params SlideshowDB) (err error) {
	sqlStr := fmt.Sprintf("INSERT INTO home_admin_module SET resourceid = 0, `module` = %d, title = '%s', `explain` = '%s', url = '%s', `status` = %d, sequence = %d, operator = %d, ",
		params.Module, params.Title, params.Explain, params.Url, params.Status, params.Sequence, uid)
	if len(params.Pic) > 0 {
		sqlStr += fmt.Sprintf("pic = '%s', ", params.Pic)
	}
	sqlStr += fmt.Sprintf("dateline = %d", time.Now().Unix())
	Log("info", "正在添加海报 uid=", uid, params)
	res := sqlx.MustExec(config.C.DB.DB, sqlStr)
	_, err = res.RowsAffected()
	if err != nil {
		Log("info", "添加海报失败 err=", uid)
		Log("err", err.Error())
		return errno.DbError
	}
	Log("info", "添加海报成功")
	return
}

func UpdateSlideshow(uid int64, params SlideshowDB) (err error) {
	if params.ID <= 0 {
		return errors.New("id不能为空")
	}
	sqlStr := fmt.Sprintf("UPDATE home_admin_module SET `module` = %d, title = '%s', `explain` = '%s', url = '%s', `status` = %d, sequence = %d, ",
		params.Module, params.Title, params.Explain, params.Url, params.Status, params.Sequence)
	if len(params.Pic) > 0 {
		sqlStr += fmt.Sprintf("pic = '%s', ", params.Pic)
	}
	sqlStr += fmt.Sprintf("operator = %d WHERE id = %d", uid, params.ID)

	res := sqlx.MustExec(config.C.DB.DB, sqlStr)
	_, err = res.RowsAffected()
	if err != nil {
		Log("err", err.Error())
		return errno.DbError
	}
	return
}

func DeleteSlideshow(id int) (err error) {
	res := sqlx.MustExec(config.C.DB.DB, "UPDATE home_admin_module SET `status` = 0 WHERE id = ?", id)
	_, err = res.RowsAffected()
	if err != nil {
		Log("err", err.Error())
		return
	}
	return
}
